package com.zy.web.controller.business;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.zy.business.domain.TbProject;
import com.zy.business.domain.TbProjectOperRecord;
import com.zy.business.domain.TbProjectProgressRate;
import com.zy.business.domain.vo.ProjectDto;
import com.zy.business.service.ProjectService;
import com.zy.common.annotation.Log;
import com.zy.common.annotation.RepeatSubmit;
import com.zy.common.constant.HttpStatus;
import com.zy.common.core.controller.BaseController;
import com.zy.common.core.domain.AjaxResult;
import com.zy.common.core.domain.entity.SysUser;
import com.zy.common.core.page.PageDomain;
import com.zy.common.core.page.TableDataInfo;
import com.zy.common.core.page.TableSupport;
import com.zy.common.enums.BusinessType;
import com.zy.common.utils.SecurityUtils;
import com.zy.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 项目管理
 * @author: whf
 * @create: 2023-01-10 14:24
 **/
@RestController
@Api(tags = "项目管理接口")
@RequestMapping("/project")
public class ProjectController extends BaseController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ISysUserService userService;

    @ApiOperation("获取项目列表")
    @GetMapping("/list")
    public TableDataInfo list(ProjectDto project)
    {
        Long userId = SecurityUtils.getUserId();
        if(!SecurityUtils.isAdmin(userId)){
            SysUser user = userService.selectUserById(userId);
            if (Integer.parseInt(user.getOffice())==5 || Integer.parseInt(user.getOffice())==4){
                project.setUserId(userId);
            }
        }
        startPage();
        List<TbProject> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    @ApiOperation("添加项目")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping(value="/add")
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody TbProject project){
        project.setCreateBy(getUsername());
        return toAjax(projectService.addProject(project));
    }

    @ApiOperation("修改项目")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping(value="/edit")
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody TbProject project){
        project.setUpdateBy(getUsername());
        return toAjax(projectService.editProject(project));
    }

    @ApiOperation("验证项目名称是否已存在")
    @GetMapping("/verificationProjectName")
    public AjaxResult verificationProjectName(Long projectId,String name){
        Boolean flag = projectService.existedProjectName(projectId,name);
        if(flag){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("该项目名已存在！");
        }
    }

    @ApiOperation("删除项目")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectId}")
    public AjaxResult delete(Long projectId){
        return projectService.deleteProject(projectId);
    }

    @ApiOperation("添加项目进度")
    @PostMapping("/addProjectProgressRate")
    @RepeatSubmit
    public AjaxResult addProjectProgressRate(@Validated @RequestBody TbProjectProgressRate progressRate){
        progressRate.setCreateBy(getUsername());
        return toAjax(projectService.addProgressRate(progressRate));
    }

    @ApiOperation(value = "根据项目id获取项目进度列表")
    @GetMapping("/getProgressRateList")
    public TableDataInfo getProgressRateList(Long projectId){
        startPage();
        List<TbProjectProgressRate>  progressRateList = projectService.getProgressRateList(projectId);
        return getDataTable(progressRateList);
    }

    @ApiOperation("获取项目操作日志列表")
    @GetMapping("/getProjectOperList")
    public TableDataInfo getProjectOperList(Long projectId){
        startPage();
        List<TbProjectOperRecord> list = projectService.getProjectOperList(projectId);
        return getDataTable(list);
    }

    @ApiOperation("获取项目工时统计列表")
    @GetMapping("/getWorkHourList")
    public TableDataInfo getWorkHourList(Long projectId){
        PageDomain pageDomain = TableSupport.buildPageRequest();
        List<Map<String,Object>> workHourList = projectService.getWorkHourList(projectId);
        List<Map<String, Object>> collect = workHourList.stream().skip((pageDomain.getPageNum() - 1) * pageDomain.getPageSize())
                .limit(pageDomain.getPageSize())
                .collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(collect);
        rspData.setTotal(new PageInfo(workHourList).getTotal());
        return rspData;
    }

    @ApiOperation("获取部门成员数据")
    @GetMapping("/getDeptUser")
    public AjaxResult getDeptUser(){
        return projectService.getDeptUser();
    }

    @ApiOperation(value = "导出工时统计列表")
    @PostMapping(value = "/exportExl")
    public void exportWorkHourList(Long projectId, HttpServletResponse response){
        projectService.exportWorkHourList(projectId,response);
    }

    @ApiOperation(value = "当前用户的所属项目")
    @GetMapping("/getProjectSelect")
    public AjaxResult getProjectSelect(){
        List<Map<String,String>> projects = projectService.getProjectByUser();
        return AjaxResult.success(projects);
    }

    @ApiOperation(value = "获取项目详情")
    @GetMapping("/getDetail")
    public AjaxResult getDetail(Long projectId){
        return projectService.getDetail(projectId);
    }

}
